# -*- coding:utf8 -*- #
# -----------------------------------------------------------------------------------
# ProjectName:   smalleel-api-tools
# FileName:     publicApi
# Author:
# Datetime:    2022/7/6 14:32
# Description：
# -----------------------------------------------------------------------------------
import datetime
from common.util.jsonUtil.jsonUtil import read_sql_json
from common.util.mySqlUtil.mySqlUtil import select_sql
from common.util.requestUtil.requestUtil import RequestsUtil
from tool.process.publicInterface.public_api import PublicApi
from tool.process.publicInterface.sign_params import sign_params


class PublicInterFace:
    """
        辰鳗公共接口
    """

    def __init__(self, companyName):
        appid = None
        appSecret = None
        try:
            sql = read_sql_json('openapi_sql', 'companySecret', 6, companyName)
            appid, appSecret = select_sql(sql)[0]
        except ValueError:
            print("未查询到公司密钥,请检查公司信息！")
        self.req = RequestsUtil()
        # 正式环境
        self.timestamp = str((datetime.datetime.now().timestamp() * 1000) - 10000).split('.')[0]
        # 四川辰鳗科技有限公司-101523245690
        self.publicParams = {"appSecret": appSecret, "appid": appid,
                             "timestamp": self.timestamp}

    def __get_params(self, params):
        reqParams = dict(self.publicParams, **params)
        sign = sign_params(self.publicParams, params)
        reqParams["sign"] = sign
        return reqParams

    # 1.4.1、充值记录
    def rechargerRecord(self):
        """
            1.4.1、充值记录
        :return:None
        """
        url = PublicApi.dbRechargePage
        params = {"pageNum": 1, "pageSize": 50, "startTime": "", "endTime": ""}
        return self.req.send_request(url=url, method="GET", params=self.__get_params(params))

    # 1.5.1、电表信息分页
    def dbPage(self):
        """
            1.5.1、电表信息分页
        :return:
        """
        url = PublicApi.dbPage
        params = {"pageNum": "1", "pageSize": "10", "no": "", "tagId": ""}
        return self.req.send_request(url=url, method="GET", params=self.__get_params(params))

    # 1.5.2、电表详情
    def dbDetail(self):
        """
            1.5.2、电表详情
        :return:
        """
        url = PublicApi.dbDetail
        params = {"dbNo": "102021030126", "dbId": ""}
        # params = {"dbNo": "652206010076", "dbId": ""}
        return self.req.send_request(url=url, method="GET", params=self.__get_params(params))

    # 1.5.3、电表用电量分析
    def powerAnalysis(self):
        """
            1.5.3、电表用电量分析
        :return:
        """
        url = PublicApi.getPowerAnalysis
        params = {"dbNos": [102021030126]}
        return self.req.send_request(url=url, method="GET", params=self.__get_params(params))

    # 1.5.4、充值
    def recharge(self):
        """
            1.5.4、充值
        :return:
        """
        url = PublicApi.recharge
        # 线上
        params = {"dbNo": "102021030126", "amount": 200}
        # 测试
        # params = {"dbNo": "112232908539", "amount": 200}
        return self.req.send_request(url=url, method="POST", params=self.__get_params(params),
                                     parameterFormat="data")

    # 1.5.5、调整余额
    def updateBalance(self):
        """
            1.5.5、调整余额
        :return:
        """
        url = PublicApi.updBalance
        params = {"dbNo": "102021030126", "balance": -10, "bz": ""}
        return self.req.send_request(url=url, method="POST", params=self.__get_params(params),
                                     parameterFormat="data")

    # 1.5.6、修改电价
    def batchUpdDj(self, priceType: int = 1):
        """
            1.5.6、修改电价
            priceType：1固定电价，2分时电价，3阶梯电价
            priceContent：电价内容：固定电价直接填电价
            阶梯电价：
                {
                '1':{amount:200,price:1.2}
                '2':{amount:300,price:1.3}
                '3':{amount:400,price:1.4}
                }
            复费率电价：
                {
                '0':{price:1.2,type:1},
                '1':{price:1.2,type:1},
                '2':{price:1.2,type:1},
                '3':{price:1.2,type:1},
                ...
                }
        :return:
        """
        priceContent = None
        dbNos = "102021030126"
        url = PublicApi.batchUpdDj
        if priceType == 1:
            # 固定电价
            priceContent = 2
        elif priceType == 2:
            # 分时电价
            priceContent = {
                "0": {"price": 1.2, "type": 1}, "1": {"price": 1.2, "type": 1},
                "2": {"price": 1.3, "type": 2}, "3": {"price": 1.3, "type": 2},
                "4": {"price": 1.5, "type": 3}, "5": {"price": 1.7, "type": 4},
                "6": {"price": 1.7, "type": 4}, "7": {"price": 1.7, "type": 4},
                "8": {"price": 1.2, "type": 1}, "9": {"price": 1.2, "type": 1},
                "10": {"price": 1.2, "type": 1}, "11": {"price": 1.2, "type": 1},
                "12": {"price": 1.2, "type": 1}, "13": {"price": 1.2, "type": 1},
                "14": {"price": 1.3, "type": 2}, "15": {"price": 1.2, "type": 1},
                "16": {"price": 1.5, "type": 3}, "17": {"price": 1.2, "type": 1},
                "18": {"price": 1.7, "type": 4}, "19": {"price": 1.2, "type": 1},
                "20": {"price": 1.2, "type": 1}, "21": {"price": 1.2, "type": 1},
                "22": {"price": 1.2, "type": 1}, "23": {"price": 1.2, "type": 1}
            }
        elif priceType == 3:
            # 阶梯电价
            priceContent = {
                '1': {"amount": 200, "price": 1.2},
                '2': {"amount": 300, "price": 1.3},
                '3': {"amount": 400, "price": 1.4}
            }
        params = {"dbNos": dbNos, "priceType": priceType, "priceContent": str(priceContent), "plusPrice": "0.3"}
        return self.req.send_request(url=url, method="POST", params=self.__get_params(params),
                                     parameterFormat="data")

    # 1.5.7、获取电表实时数据
    def dbRealTime(self):
        """
            1.5.7、获取电表实时数据
        :return:
        """
        url = PublicApi.getDbRealTime
        params = {"dbNo": "102021030126"}

        return self.req.send_request(url=url, method="GET", params=self.__get_params(params))


if __name__ == '__main__':
    api = PublicInterFace("测试公司移动到区域")
    # api = PublicApi("嘉诚新悦物业华北区域")
    # 1.4.1、充值记录
    # print(api.rechargerRecord())
    # 1.5.1、电表信息分页
    # print(api.dbPage())
    # 1.5.2、电表详情
    # print(api.dbDetail())
    # 1.5.3、电表用电量分析
    # print(api.powerAnalysis())
    # 1.5.4、充值
    print(api.recharge())
    # 1.5.5、调整余额
    print(api.updateBalance())
    # 1.5.6、修改电价
    print(api.batchUpdDj(3))
    # 1.5.7、获取电表实时数据
    print(api.dbRealTime())
